﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.Updater.Common.Configurations.Server;
using PSE.Updater.Common.Gateway.DataContract;
using PSE.Updater.Common.Providers.Interfaces;
using PSE.Updater.Common.Providers;
using PSE.Updater.Server.Engine;
using System.ServiceModel;
using System.ServiceModel.Description;
using PSE.Updater.Common.Gateway;
using PSE.Updater.Common.Gateway.ServiceContract;

namespace TesteUpdater
{
    public class Providers
    {

        public string Xml { get; set; }

        public void Test()
        {
            //TestAddProduct();
            //TestAddFolder();
            //TestAlterFolder();
            //TestRemoveFolder();
            //TestAddNewFolder();
            //TestSection();
            //TestRemoveAllFolders();
            //TestRemoveProduct();

            Connect();
        }

        private void Connect()
        {
            ChannelFactory<IUpdatesRegistryProductFolders> serviceChannel = null;


            var bindings = BindingFactory.CreateNetNamedPipeBinding(TransferMode.Buffered);

            try
            {
                EndpointAddress endpointAddress = new EndpointAddress(new Uri("net.pipe://localhost/PSE.Updater.Server.Service/UpdatesRegistryProductFoldersService/"));
                serviceChannel = new ChannelFactory<IUpdatesRegistryProductFolders>(bindings, endpointAddress);

                foreach (OperationDescription op in serviceChannel.Endpoint.Contract.Operations)
                {
                    var dcSerializer = op.Behaviors.Find<DataContractSerializerOperationBehavior>();
                    if (dcSerializer != null)
                        dcSerializer.MaxItemsInObjectGraph = Int32.MaxValue;
                }

                serviceChannel.Open();

                IUpdatesRegistryProductFolders client = serviceChannel.CreateChannel();
                client.AddFolder(ProviderType.Updates, "Sinacor", @"C:\Program Files\BMFBOVESPA\Sinacor\Updates");
            }
            catch (EndpointNotFoundException ex)
            {

            }
            catch (CommunicationException ex)
            {
            }
            catch (Exception ex)
            {
            }
            finally
            {
                try
                {
                    if (serviceChannel != null && serviceChannel.State != CommunicationState.Faulted)
                        serviceChannel.Close();
                }
                catch
                {
                    serviceChannel.Abort();
                }
            }
        }

        public void TestAddProduct()
        {
            RegistryProduct.Instance.AddProduct("Teste1");
            RegistryProduct.Instance.AddProduct("Teste2");
            RegistryProduct.Instance.AddProduct("Teste3");

            RegistryProduct.Instance.Save();
        }

        public void TestAddFolder()
        {
            RegistryProduct.Instance.AddFolder(ProviderType.Updates, "Teste1", @"C:\1");
            RegistryProduct.Instance.AddFolder(ProviderType.ConfigurationFile, "Teste1", @"C:\1\File");
            RegistryProduct.Instance.AddFolder(ProviderType.Updates, "Teste1", @"C:\2");
            RegistryProduct.Instance.AddFolder(ProviderType.ConfigurationFile, "Teste1", @"C:\2\File");

            RegistryProduct.Instance.AddFolder(ProviderType.Updates, "Teste2", @"D:\1");
            RegistryProduct.Instance.AddFolder(ProviderType.ConfigurationFile, "Teste2", @"D:\1\File");
            RegistryProduct.Instance.AddFolder(ProviderType.Updates, "Teste2", @"D:\2");
            RegistryProduct.Instance.AddFolder(ProviderType.ConfigurationFile, "Teste2", @"D:\2\File");

            RegistryProduct.Instance.AddFolder(ProviderType.Updates, "Teste3", @"E:\1");
            RegistryProduct.Instance.AddFolder(ProviderType.ConfigurationFile, "Teste3", @"E:\1\File");
            RegistryProduct.Instance.AddFolder(ProviderType.Updates, "Teste3", @"E:\2");
            RegistryProduct.Instance.AddFolder(ProviderType.ConfigurationFile, "Teste3", @"E:\2\File");

            RegistryProduct.Instance.Save();
        }

        public void TestAddNewFolder()
        {
            RegistryProduct.Instance.AddFolder(ProviderType.Updates, "Teste1", @"C:\New");
            RegistryProduct.Instance.AddFolder(ProviderType.ConfigurationFile, "Teste1", @"C:\New\File");

            RegistryProduct.Instance.AddFolder(ProviderType.Updates, "Teste2", @"D:\New");
            RegistryProduct.Instance.AddFolder(ProviderType.ConfigurationFile, "Teste2", @"D:\New\File");

            RegistryProduct.Instance.AddFolder(ProviderType.Updates, "Teste3", @"E:\New");
            RegistryProduct.Instance.AddFolder(ProviderType.ConfigurationFile, "Teste3", @"E:\New\File");

            RegistryProduct.Instance.Save();
        }

        public void TestAlterFolder()
        {
            RegistryProduct.Instance.AlterFolder(ProviderType.Updates, "Teste1", @"C:\1", @"C:\1\Alter");
            RegistryProduct.Instance.AlterFolder(ProviderType.ConfigurationFile, "Teste1", @"C:\2\File", @"C:\2\File\Alter");

            RegistryProduct.Instance.AlterFolder(ProviderType.Updates, "Teste2", @"D:\1", @"D:\1\Alter");
            RegistryProduct.Instance.AlterFolder(ProviderType.ConfigurationFile, "Teste2", @"D:\2\File", @"D:\2\File\Alter");

            RegistryProduct.Instance.AlterFolder(ProviderType.Updates, "Teste3", @"E:\1", @"E:\1\Alter");
            RegistryProduct.Instance.AlterFolder(ProviderType.ConfigurationFile, "Teste3", @"E:\2\File", @"E:\2\File\Alter");

            RegistryProduct.Instance.Save();
        }

        public void TestRemoveFolder()
        {
            RegistryProduct.Instance.RemoveFolder(ProviderType.Updates, "Teste1", @"C:\2");
            RegistryProduct.Instance.RemoveFolder(ProviderType.ConfigurationFile, "Teste1", @"C:\1\File");

            RegistryProduct.Instance.RemoveFolder(ProviderType.Updates, "Teste2", @"D:\2");
            RegistryProduct.Instance.RemoveFolder(ProviderType.ConfigurationFile, "Teste2", @"D:\1\File");

            RegistryProduct.Instance.RemoveFolder(ProviderType.Updates, "Teste3", @"E:\2");
            RegistryProduct.Instance.RemoveFolder(ProviderType.ConfigurationFile, "Teste3", @"E:\1\File");

            RegistryProduct.Instance.Save();
        }

        public void TestRemoveAllFolders()
        {
            RegistryProduct.Instance.RemoveAllFolders(ProviderType.Updates, "Teste1");
            RegistryProduct.Instance.RemoveAllFolders(ProviderType.Updates, "Teste2");
            RegistryProduct.Instance.RemoveAllFolders(ProviderType.Updates, "Teste3");
            RegistryProduct.Instance.Save();
        }

        public void TestRemoveProduct()
        {
            RegistryProduct.Instance.RemoveProduct("Teste1");
            RegistryProduct.Instance.RemoveProduct("Teste2");
            RegistryProduct.Instance.RemoveProduct("Teste3");
            RegistryProduct.Instance.Save();
        }

        private void TestSection()
        {
            var section = UpdateProviders.Instance.GetSection();
            foreach (ProductConfigurationElement product in section.Products)
            {
                foreach (ProviderConfigurationElement provider in product.Providers)
                {
                    foreach (WatcherConfigurationElement observer in provider.Watchers)
                    {
                        var directoryTemplate = observer.DirectoryTemplate;
                        var silentMode = observer.SilentMode.ToString();

                        var extensionsFilter = observer.ExtensionsFilter;
                        var recursive = observer.Recursive;
                    }
                }
            }
        }
    }
}
